package com.gzy.android_douban.ui.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.gzy.android_douban.data.api.ApiService;
import com.gzy.android_douban.data.model.UsersLoginModel;
import com.gzy.android_douban.data.model.UsersRegisteredModel;
import com.gzy.android_douban.databinding.ActivityRegisterBinding;
import com.gzy.android_douban.ui.home.HomeFragment;
import com.gzy.android_douban.ui.home.SearchActivity;
import com.gzy.network.NetworkApi;
import com.gzy.network.observer.BaseObserver;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RegisterActivity extends AppCompatActivity {
    ActivityRegisterBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initClick();
    }

    private void initClick() {
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("password",binding.edPassword.getText().toString());
                    jsonObject.put("username",binding.edUsername.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                RequestBody body= RequestBody.create(MediaType.parse("application/json"),jsonObject.toString());

                NetworkApi.createService(ApiService.class)
                        .postRegisteredApi(body)
                        .compose(NetworkApi.applySchedulers(new BaseObserver<UsersRegisteredModel>() {

                            @Override
                            public void onSuccess(UsersRegisteredModel usersRegisteredModel) {
                                System.out.println("postRegisteredApi 请求成功");
                                if (usersRegisteredModel.getCode() == 200){
                                    Toast.makeText(RegisterActivity.this, "返回主页", Toast.LENGTH_SHORT).show();
                                    Intent intent =  new Intent(RegisterActivity.this, LoginActivity.class);
                                    intent.putExtra("username",binding.edUsername.getText().toString());
                                    startActivity(intent);
                                    finish();
                                }else{
                                    System.out.println(usersRegisteredModel.getCode());
                                }

                            }

                            @Override
                            public void onFailure(Throwable e) {
                                System.out.println("postRegisteredApi 请求失败"+e.getMessage());
                            }
                        }));
            }
        });

        binding.imgBackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this, "返回主页", Toast.LENGTH_SHORT).show();
                Intent intent =  new Intent(RegisterActivity.this, HomeFragment.class);
                finish();
            }
        });

    }
}